<?php
declare (strict_types = 1);
namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Cache;
use app\model\UserModel;
use app\model\DeptModel;
use app\controller\Order;


//分配管理类
class Log{

    var $dept=[];
    var $users=[];

    public function __construct(){
        $um=new UserModel();
        $this->users = $um->getAll();
        
        $dm=new DeptModel();
        $this->dept = $dm->getAll();

        $order = new Order();
        $this->ost=$order->status;

    }


    public function index(){

    }

    //分配记录
    public function log($table='follow_log'){
        $all=Request::param();

        if(!empty($all['table'])) $table=$all['table'];

        $list=[];
        if(!empty($all['customer_id'])){
            $list =Db::table($table)->where('customer_id',$all['customer_id'])->order('create_time','desc')->select()->toArray();
        }
        foreach($list as &$row){
            if(!empty($row['trainer_id'])){
                $row['trainer_id_name'] = Db::table('customer')->where('id',$row['trainer_id'])->value('name');
            }
            if(!empty($row['dept_id'])){
                $row['dept_id_name'] = $this->dept[$row['dept_id']]['name'];
            }

            $row['user_id_name'] = $this->users[$row['user_id']]['name'];

        }

        return json($list);

    }

    public function order_list($customer_id){
        $all=Request::param();
        $list =Db::table('order')->where('customer_id',$all['customer_id'])->order('create_time','desc')->select()->toArray();

        foreach($list as &$row){
            if(!empty($row['trainer_id'])){
                $row['trainer_id_name'] = Db::table('customer')->where('id',$row['trainer_id'])->value('name');
            }
            if(!empty($row['dept_id'])){
                $row['dept_id_name'] = $this->dept[$row['dept_id']]['name'];
            }

            $row['status_name']=$this->ost[$row['status']][0];

        }

        return json($list);  

    }


    public function acc_log($customer_id){
        $status=array('1'=>'待审核','2'=>'已审核','4'=>'已取消');
        $acc_type=array(1=>'学费(收款)',2=>'补考费(收款)',3=>'普通收款',4=>'红包',5=>'推广费',6=>'其他付款',7=>'提现');        

        $all=Request::param();

        $list =Db::table('acc_log')->where('customer_id',$all['customer_id'])->order('create_time','desc')->select()->toArray();

        foreach($list as &$row){

            if(!empty($row['dept_id'])){
                $row['dept_id_name'] = $this->dept[$row['dept_id']]['name'];
            }

            $row['status_name']=$status[$row['status']];
            $row['type_name']=$acc_type[$row['type']];

        }

        return json($list);  

    }

    public function new_log($arr=[]){

        $res =Db::table('allocate_log')->insert($arr);
        return $res;

    }

}
